home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / shareware / share_41 / assembler / examples / sort < prev    next >
Text File  |  1991-05-17  |  1KB  |  60 lines

  1. ; NAME       sort
  2. ; PURPOSE    sorts the characters in text1
  3. ; DESIGN    
  4. ;            count nunber of characters
  5. ;            repeat
  6. ;              done <- true
  7. ;              for i = 0 to number of characters
  8. ;                if character[i+1] > character[i] 
  9. ;                then
  10. ;                   swap character[i+1] with character[i] 
  11. ;                   done <- false
  12. ;                end if
  13. ;              end for
  14. ;            until done
  15.  
  16.  
  17.  
  18. ; sorts the letters in a string
  19. ; based on bubble sort
  20.  
  21. ; first count the number of characters
  22.  
  23. mov     r0, #0
  24. sub     r0, r0, #1
  25. mov     r6, #0
  26. .count_loop
  27.    add    r0, r0, #1
  28.    ldrb   r6, [r4, r0]
  29.    cmp    r6, #31
  30. bpl       count_loop
  31.  
  32.  
  33. ; now sort them
  34. mov      r6, #0
  35. mov      r7, #0
  36. mov      r1, #1
  37. mov      r2, #0
  38. add      r5, r4, #1
  39. sub      r0, r0, #1
  40.  
  41. .repeat 
  42.    mov      r1, #1
  43.    mov      r2, #0
  44.    .for
  45.       ldrb    r6, [r4, r2]
  46.       ldrb    r7, [r5, r2]
  47.       cmp     r7, r6
  48.       bpl     no_swap
  49.          mov     r8, r6
  50.          mov     r6, r7
  51.          mov     r7, r8
  52.          strb    r6, [r4, r2]
  53.          strb    r7, [r5, r2]
  54.          mov     r1, #0
  55.       .no_swap
  56.       add     r2, r2, #1
  57.       cmp     r2, r0
  58.    bmi     for
  59.    cmp     r1, #1
  60. bne     repeat